package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class m {
    static class a extends Handler {
        private final WeakReference a;

        public a(m arg2) {

            this.a = new WeakReference(arg2);
        }

        public void handleMessage(Message arg5) {
            if (this.a != null) {
                switch (arg5.what) {
                    case 48: {
                        this.sendEmptyMessageDelayed(48, q.c(System.currentTimeMillis()));
                        com.umeng.analytics.pro.m.c(com.umeng.analytics.pro.m.a(com.umeng.analytics.pro.m.g()));
                        return;
                    }
                    case 49: {
                        this.sendEmptyMessageDelayed(49, q.d(System.currentTimeMillis()));
                        com.umeng.analytics.pro.m.d(com.umeng.analytics.pro.m.a(com.umeng.analytics.pro.m.g()));
                        return;
                    }
                }
            }
        }
    }

    static class m$b {
        private static final m a = new m();

        private m$b() {

        }

        static m a() {
            return m$b.a;
        }
    }

    private h a;
    private o b;
    private p c;
    private boolean d;
    private boolean e;
    private long f;
    private final String g;
    private final String h;
    private static final int i = 48;
    private static final int j = 49;
    private static Context k;
    private List l;
    private a m;
    private final Thread n;

    private m() {
        this.a = null;
        this.b = null;
        this.c = null;
        this.d = false;
        this.e = false;
        this.f = 0;
        this.g = "main_fest_mode";
        this.h = "main_fest_timestamp";
        this.l = new ArrayList();
        this.m = null;
        this.n = new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                if (com.umeng.analytics.pro.m.a(m.this) == null) {
                    com.umeng.analytics.pro.m.a(m.this, new com.umeng.analytics.pro.m.a(m.this));
                }

                com.umeng.analytics.pro.m.b(m.this);
            }
        });
        if (k != null) {
            if (this.a == null) {
                this.a = new h();
            }
            if (this.b == null) {
                this.b = o.a(k);
            }
            if (this.c != null) {
            } else {
                this.c = new p();
            }
        }
        this.n.start();
    }

    public void a(final f arg2) {
        if (!this.d) {
            bz.b(new cb() {
                @Override
                public void a() {
                    try {
                        com.umeng.analytics.pro.m.f(m.this).a(new f() {
                            public void a(Object arg3, boolean arg4) {
                                if ((arg3 instanceof Map)) {
                                    com.umeng.analytics.pro.m.e(m.this).a(((Map) arg3));
                                }

                                com.umeng.analytics.pro.m.a(m.this, true);
                            }
                        });
                        com.umeng.analytics.pro.m.g(m.this);
                        com.umeng.analytics.pro.m.h(m.this);
                        arg2.a("success", false);
                    } catch (Exception v0) {
                        v0.printStackTrace();
                    }
                }
            });
        }
    }

    public static final m a(Context arg1) {
        k = arg1;
        return com.umeng.analytics.pro.m.m$b.a();
    }

    public void a(final f fVar, Map<List<String>, l> arg6) {
        Object v0 = arg6.values().toArray()[0];
        List v1 = ((l) v0).a();
        if (this.l.size() > 0 && (this.l.contains(com.umeng.analytics.pro.d.a(v1)))) {
            this.a.a(new f() {
                public void a(Object arg4, boolean arg5) {
                    if ((arg4 instanceof h)) {
                        com.umeng.analytics.pro.m.a(m.this, ((h) arg4));
                    }

                    fVar.a("success", false);
                }
            }, ((l) v0));
        } else if (this.e) {
            this.a(((l) v0), v1);
        } else if (this.i()) {
            String v2 = com.umeng.analytics.pro.d.a(v1);
            if (!this.l.contains(v2)) {
                this.l.add(v2);
            }
            this.a.a(new f() {
                public void a(Object arg2, boolean arg3) {
                    com.umeng.analytics.pro.m.a(m.this, ((h) arg2));
                }
            }, v1, ((l) v0));
        } else {
            this.a(((l) v0), v1);
            this.j();
        }
    }

    public void a(long arg10, long arg12, String arg14) {
        this.b.a(new f() {
            public void a(Object arg2, boolean arg3) {
                arg2.equals("success");
            }
        }, arg14, arg10, arg12);
    }

    static p a(m arg0, p arg1) {
        arg0.c = arg1;
        return arg1;
    }

    static a a(m arg1) {
        return arg1.m;
    }

    static a a(m arg0, a arg1) {
        arg0.m = arg1;
        return arg1;
    }

    static boolean a(m arg0, boolean arg1) {
        arg0.d = arg1;
        return arg1;
    }

    static h a(m arg0, h arg1) {
        arg0.a = arg1;
        return arg1;
    }

    private void a(l arg4, List arg5) {
        this.a.a(new f() {
            public void a(Object arg2, boolean arg3) {
                if ((arg2 instanceof h)) {
                    com.umeng.analytics.pro.m.a(m.this, ((h) arg2));
                } else if ((arg2 instanceof Boolean)) {
                    com.umeng.analytics.pro.m.i(m.this);
                }
            }
        }, arg4, arg5, this.l);
    }

    public boolean a() {
        return this.d;
    }

    public JSONObject b() {
        JSONObject v0_2;
        JSONObject v2 = this.b.a();
        JSONObject v1 = new JSONObject();
        if (v2 != null && v2.length() > 0) {
            Iterator v3 = this.l.iterator();
            while (true) {
                if (v3.hasNext()) {
                    Object v0 = v3.next();
                    if (v2.has(((String) v0))) {
                        try {
                            v1.put(((String) v0), v2.opt(((String) v0)));
                        } catch (Exception v0_1) {
                        }
                        continue;
                    } else {
                        continue;

                    }
                } else {
                    break;
                }
            }
            v0_2 = v1;
        } else {
            v0_2 = null;
        }

        return v0_2;
    }

    public void b(f arg5) {
        boolean v0 = false;
        if (this.e) {
            if (this.f == 0) {
                this.l();
            }

            v0 = q.a(System.currentTimeMillis(), this.f);
        }

        if (!v0) {
            this.k();
            this.l.clear();
        }

        this.c.b();
        this.b.a(new f() {
            public void a(Object arg2, boolean arg3) {
                if (arg2.equals("success")) {
                    com.umeng.analytics.pro.m.k(m.this);
                }
            }
        }, v0);
    }

    static void b(m arg0) {
        arg0.h();
    }

    public JSONObject c() {
        if (this.c.a().size() > 0) {
            this.b.b(new f() {
                public void a(Object arg2, boolean arg3) {
                    if ((arg2 instanceof String)) {
                        com.umeng.analytics.pro.m.j(m.this).b();
                    }
                }
            }, this.c.a());
        }

        return this.b.b(new f());
    }

    static void c(m arg0) {
        arg0.p();
    }

    public void d() {
        this.p();
    }

    static void d(m arg0) {
        arg0.o();
    }

    public void e() {
        this.p();
    }

    static h e(m arg1) {
        return arg1.a;
    }

    public void f() {
        this.p();
    }

    static o f(m arg1) {
        return arg1.b;
    }

    static void g(m arg0) {
        arg0.l();
    }

    static Context g() {
        return com.umeng.analytics.pro.m.k;
    }

    static void h(m arg0) {
        arg0.q();
    }

    private void h() {
        long v0 = System.currentTimeMillis();
        this.m.sendEmptyMessageDelayed(48, q.c(v0));
        this.m.sendEmptyMessageDelayed(49, q.d(v0));
    }

    static void i(m arg0) {
        arg0.n();
    }

    private boolean i() {
        boolean v0 = this.l.size() < com.umeng.analytics.pro.n.a().d() ? true : false;
        return v0;
    }

    static p j(m arg1) {
        return arg1.c;
    }

    private void j() {
        SharedPreferences v0 = ba.a(k);
        if (!v0.getBoolean("main_fest_mode", false)) {
            this.e = true;
            SharedPreferences.Editor v0_1 = v0.edit();
            v0_1.putBoolean("main_fest_mode", true);
            v0_1.putLong("main_fest_timestamp", System.currentTimeMillis());
            v0_1.commit();
        }
    }

    static void k(m arg0) {
        arg0.m();
    }

    private void k() {
        SharedPreferences.Editor v0 = ba.a(k).edit();
        v0.putBoolean("main_fest_mode", false);
        v0.putLong("main_fest_timestamp", 0);
        v0.commit();
        this.e = false;
    }

    private void l() {
        SharedPreferences v0 = ba.a(k);
        this.e = v0.getBoolean("main_fest_mode", false);
        this.f = v0.getLong("main_fest_timestamp", 0);
    }

    private void m() {
        Iterator v1 = this.a.a().entrySet().iterator();
        while (v1.hasNext()) {
            List v0 = (List) ((Map.Entry) v1.next()).getKey();
            if (this.l.contains(v0)) {
                continue;
            }
            this.l.add(com.umeng.analytics.pro.d.a(((List) v0)));
        }

        if (this.l.size() > 0) {
            this.b.a(new f(), this.l);
        }
    }

    private void n() {
        this.c.a(new f() {
            public void a(Object arg2, boolean arg3) {
                com.umeng.analytics.pro.m.a(m.this, ((p) arg2));
            }
        }, "__ag_of");
    }

    private void o() {
        try {
            if (this.a.a().size() > 0) {
                this.b.c(new f() {
                    public void a(Object arg2, boolean arg3) {
                        if ((arg2 instanceof String)) {
                            com.umeng.analytics.pro.m.e(m.this).d();
                        }
                    }
                }, this.a.a());
            }

            if (this.c.a().size() > 0) {
                this.b.b(new f() {
                    public void a(Object arg2, boolean arg3) {
                        if ((arg2 instanceof String)) {
                            com.umeng.analytics.pro.m.j(m.this).b();
                        }
                    }
                }, this.c.a());
            }

            if (this.l.size() <= 0) {
                return;
            }

            this.b.a(new f(), this.l);
        } catch (Throwable v0) {
            by.b("converyMemoryToDataTable happen error: " + v0.toString());
        }
    }

    private void p() {
        try {
            if (this.a.a().size() > 0) {
                this.b.a(new f() {
                    public void a(Object arg1, boolean arg2) {
                    }
                }, this.a.a());
            }

            if (this.c.a().size() > 0) {
                this.b.b(new f() {
                    public void a(Object arg2, boolean arg3) {
                        if ((arg2 instanceof String)) {
                            com.umeng.analytics.pro.m.j(m.this).b();
                        }
                    }
                }, this.c.a());
            }

            if (this.l.size() <= 0) {
                return;
            }

            this.b.a(new f(), this.l);
        } catch (Throwable v0) {
            by.b("convertMemoryToCacheTable happen error: " + v0.toString());
        }
    }

    private void q() {
        List v0 = this.b.b();
        if (v0 != null) {
            this.l = v0;
        }
    }
}

